package com.hwd.gmall.payment.controller;

import com.alibaba.fastjson.JSONObject;
import com.hwd.gmall.payment.service.ZfbPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 支付宝支付业务的控制层
 *
 * @author 黄伟东/Victor
 * @date 2022/5/10 20:32
 */
@RestController
@RequestMapping(value = "/zfb/pay")
public class ZfbPayController {

    @Resource
    private ZfbPayService zfbPayService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 获取支付宝支付二维码的接口
     *
     * @param orderId     订单id
     * @param money       订单金额
     * @param description 订单描述n
     * @return java.lang.String
     */
    @GetMapping(value = "/getPayUrl")
    public String getPayUrl(String orderId, String money, String description) {
        return zfbPayService.getZfbPayUrl(orderId, money, description);
    }

    /**
     * 获取订单的支付结果
     *
     * @param orderId 订单id
     * @return java.lang.String
     */
    @GetMapping(value = "/getZfbPayResult")
    public String getZfbPayResult(String orderId) {
        return zfbPayService.getZfbPayResult(orderId);
    }

    /**
     * 支付宝异步通知的地址
     *
     * @param resultMap 结果
     * @return java.lang.String
     */
    @PostMapping(value = "/callback/notify")
    public String callbackNotify(@RequestParam Map<String, String> resultMap) {
        rabbitTemplate.convertAndSend(
                "order_pay_exchange",
                "pay.order",
                JSONObject.toJSONString(resultMap));
        return "success";
    }

    /**
     * 同步回调
     *
     * @param resultMap 结果
     * @return java.lang.String
     */
    @GetMapping(value = "/callback/return")
    public String callbackReturn(@RequestParam Map<String, String> resultMap) {
        System.out.println("同步回调的内容为: " + resultMap);
        return "同步回调完成,我从支付宝跳回来了!";
    }
}
